package com.have.blog.admin.controller;


import com.have.blog.admin.domain.dto.BlogCategoryDto;
import com.have.blog.admin.domain.dto.BlogTagDto;
import com.have.blog.admin.service.AdminBlogService;
import com.have.blog.admin.service.AdminIndexService;
import com.have.blog.admin.service.AdminWebVisitService;
import com.have.blog.base.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "首页相关接口", tags = {"首页相关接口"})
@RequestMapping("/index")
@Slf4j
public class AdminIndexController {

    @Autowired
    private AdminIndexService adminIndexService;
    @Autowired
    private AdminWebVisitService webVisitService;
    @Autowired
    private AdminBlogService blogService;

    @ApiOperation(value = "首页初始化数据", notes = "首页初始化数据")
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public Wrapper init() {
        return adminIndexService.init();
    }

    @ApiOperation(value = "获取最近一周用户独立IP数和访问量", notes = "获取最近一周用户独立IP数和访问量")
    @RequestMapping(value = "/getVisitByWeek", method = RequestMethod.GET)
    public Wrapper<Map<String, Object>> getVisitByWeek() {
        return webVisitService.getVisitByWeek();
    }

    @ApiOperation(value = "获取每个标签下文章数目", notes = "获取每个标签下文章数目")
    @RequestMapping(value = "/getBlogCountByTag", method = RequestMethod.GET)
    public Wrapper getBlogCountByTag() {
        List<BlogTagDto> blogCountByTag = blogService.getBlogCountByTag();
        List<Map<String, Object>> result = new ArrayList<>();
        for (BlogTagDto dto : blogCountByTag) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("name", dto.getContent());
            temp.put("value", dto.getCount());
            temp.put("tagUid", dto.getUid());
            result.add(temp);
        }
        return Wrapper.success(result);
    }

    @ApiOperation(value = "获取每个分类下文章数目", notes = "获取每个分类下文章数目")
    @RequestMapping(value = "/getBlogCountByCategory", method = RequestMethod.GET)
    public Wrapper getBlogCountByCategory(){
        List<BlogCategoryDto> blogCountByCategory = blogService.getBlogCountByCategory();
        List<Map<String, Object>> result = new ArrayList<>();
        for (BlogCategoryDto blogCategoryDto : blogCountByCategory) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("name", blogCategoryDto.getCategoryName());
            temp.put("value", blogCategoryDto.getCount());
            temp.put("blogSortUid", blogCategoryDto.getUid());
            result.add(temp);
        }
        return Wrapper.success(result);
    }

    @ApiOperation(value = "获取一年内的文章贡献数", notes = "获取一年内的文章贡献度")
    @RequestMapping(value = "/getBlogContributeCount", method = RequestMethod.GET)
    public Wrapper getBlogContributeCount() {
        return Wrapper.success(blogService.getBlogContributeCount());
    }
}
